﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using GPSTrackingWebsiteManager.GPSTrackingController;
using GPSTrackingWebsiteManager.ClassManager;
using Telerik.Web.UI;

namespace GPSTrackingWebsiteManager.Manager.Pages
{
    public partial class VPCResumenByBusStops : ClassManager.SessionManagerPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                beginDateSelected.SelectedDate = DateTime.Now.Date;
                endDateSelected.SelectedDate = DateTime.Now.Date;

                GridSortExpression expression = new GridSortExpression();
                expression.FieldName = GPSTrackingDBV2.Entities.VpcControlColumn.NumCard.ToString();
                expression.SetSortOrder("Ascending");
                this.rgResumenBusRoutesGrid.MasterTableView.SortExpressions.AddSortExpression(expression);

            }
        }

        public bool ResumenBusRoutesPeopleInOutByDateByCompanyID()
        {
            int companyID = -1;

            SessionManager objSessionManager = getSessionManager();

            if(objSessionManager != null)
                companyID = objSessionManager.CompanyIDSelectedComboBox;

            try
            {
                if (beginDateSelected.SelectedDate != null)
                {
                    if (!isDate(beginDateSelected.SelectedDate.Value.ToString()))
                    {
                        showWarningMessage("La fecha de inicio seleccionada no es válida para la consulta.");
                        return false;
                    }
                }
                else {
                    showInfoMessage("No se ha seleccionado la fecha de inicio para la consulta.");
                    return false;
                }

                if (endDateSelected.SelectedDate != null)
                {
                    if (!isDate(endDateSelected.SelectedDate.Value.ToString()))
                    {
                        showWarningMessage("La fecha de fin seleccionada no es válida para la consulta.");
                        return false;
                    }
                }
                else {
                    showInfoMessage("No se ha seleccionado la fecha de fin para la consulta.");
                    return false;
                }

                if (beginDateSelected.SelectedDate.Value > endDateSelected.SelectedDate.Value) {
                    showWarningMessage("La fecha de inicio no puede ser mayor a la fecha de fin.");
                    return false;
                }

                rgResumenBusRoutesGrid.DataSource = GPSTrackingDBV2Custom.Data.DataRepository.VPCProcessDetailsProvider.GetBusStopsByDatesByCompanyID(beginDateSelected.SelectedDate.Value, endDateSelected.SelectedDate.Value, objSessionManager.CompanyIDSelectedComboBox);
                return true;
            }
            catch (Exception ex)
            {
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                showErrorMessage("No se pudo obtener la información consultada.");
                return false;
            }
        }

        public bool isDate(string date) {
            try {
                DateTime.Parse(date);
                return true;
            } catch {
                return false;
            }
        }

        protected void rgResumenBusRoutesGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            ResumenBusRoutesPeopleInOutByDateByCompanyID();
            resizeGrid();
        }

        protected void btnResumenBusRoutesSearch_Click(object sender, EventArgs e)
        {
            if(ResumenBusRoutesPeopleInOutByDateByCompanyID())
                rgResumenBusRoutesGrid.DataBind();
            resizeGrid();
        }

        protected void lbClearFilters_Click(object sender, EventArgs e)
        {
            rgResumenBusRoutesGrid.MasterTableView.FilterExpression = string.Empty;

            foreach (GridColumn column in rgResumenBusRoutesGrid.MasterTableView.RenderColumns)
            {
                if (column is GridBoundColumn)
                {
                    GridBoundColumn boundColumn = column as GridBoundColumn;
                    boundColumn.CurrentFilterValue = string.Empty;
                }
            }
            rgResumenBusRoutesGrid.MasterTableView.Rebind();
        }

        protected void resizeGrid()
        {
            rgResumenBusRoutesGrid.Height = Unit.Parse((float.Parse(HiddenFieldHeight.Value.ToString()) - 125).ToString());
        }

        protected SessionManager getSessionManager()
        {
            return (SessionManager)HttpContext.Current.Session["SessionManager"];
        }

        protected void lbShowError_Click(object sender, EventArgs e)
        {
            showErrorMessage(HiddenFieldError.Value);
        }

        public void showErrorMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_Error", "objWindowsMessageManager.show_Error('" + msg + "','Resumen de Rutas...');", true);
        }

        public void showInfoMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_Info", "objWindowsMessageManager.show_Info('" + msg + "','Resumen de Rutas...');", true);
        }

        public void showWarningMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_Warning", "objWindowsMessageManager.show_Warning('" + msg + "','Resumen de Rutas...');", true);
        }
    }
}